//
//  CheckInStateVC.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/16.
//  保安管理-签到、签退成功页面

import Foundation
import UIKit

class CheckInSuccessVC: SubLevelViewController {
    
    var timer: Timer!
    var count: NSInteger = 3
    
    var isQT: Bool?
    
    var didDoTaskWith:(() -> ())?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "签到"
        setupUI()
        createTimer()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        stopTimer()
    }
    
    override func backToPrevious() {
        self.navigationController?.popToRootViewController(animated: true)
    }
    
    deinit {
        stopTimer()
    }
    
    func update(withSignStatus status: Bool? = nil, selectDept: DeptModal? = nil) {
        checkSuccessLabel.text = status == true ? "签退成功" : "签到成功"
        addressLabel.text = selectDept?.deptPosition
        addressDetailLabel.text = "(\(selectDept?.deptName ?? "上海银行"))"
        checkInTimeLabel.text = Date.nowCN()
        isQT = status
        doTaskButton.isHidden = status == true
    }
    
    @objc func doTaskTapped() {
        stopTimer()
        if isQT == true {
            self.backToPrevious()
        } else {
            didDoTaskWith?()
        }
    }
    
    // 实例化方法
    func createTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(countdown), userInfo: nil, repeats: true)
    }
    
    // 停止计数
    func stopTimer() {
        timer?.fireDate = Date.distantFuture
        if (timer != nil) {
            if timer.isValid {
                timer.invalidate()
                timer = nil
            }
        }
        count = 0
    }
        
    // 计数方法
    @objc func countdown() {
        if count == 0 {
            doTaskTapped()
            return
        }
        count -= 1
        tipLabel.text = "\(count)s自动返回\(isQT == true ? "" : "任务列表")"
    }
    
    // MARK: - Setup
    
    func setupUI() {
        self.view.addSubview(checkInImageView)
        self.view.addSubview(checkInTimeLabel)
        self.view.addSubview(checkSuccessLabel)
        self.view.addSubview(doTaskButton)
        self.view.addSubview(tipLabel)
        self.view.addSubview(addressPanel)
        addressPanel.addSubview(addressLabel)
        addressPanel.addSubview(addressDetailLabel)
        
        checkInImageView.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(65)
        }
        
        checkInTimeLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(checkInImageView.snp_bottomMargin).offset(50)
        }
        
        checkSuccessLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.checkInTimeLabel.snp_bottomMargin).offset(34)
        }
        
        doTaskButton.addTarget(self, action: #selector(doTaskTapped), for: .touchUpInside)
        doTaskButton.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.checkSuccessLabel.snp.bottom).offset(94)
            make.height.equalTo(50)
            make.width.equalTo(220)
        }
        
        tipLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(doTaskButton.snp.bottom).offset(25)
        }
        
        addressPanel.snp.makeConstraints { make in
            make.width.equalTo(318)
            make.centerX.equalTo(self.view)
            make.bottom.equalToSuperview().offset(-10)
        }
        
         addressLabel.snp.makeConstraints { make in
             make.top.equalToSuperview().offset(12)
             make.centerX.equalToSuperview()
             make.left.equalToSuperview().offset(10)
             make.right.equalToSuperview().offset(-10)
         }
         
         addressDetailLabel.snp.makeConstraints { make in
             make.centerX.equalToSuperview()
             make.top.equalTo(addressLabel.snp.bottom).offset(12)
             make.bottom.equalToSuperview().offset(-12)
         }
    }
    
    lazy var checkInImageView: UIImageView = {
        let imageView = UIImageView(frame: .zero)
        imageView.image = UIImage.init(named: "checkin_success")
        imageView.contentMode = UIView.ContentMode.scaleAspectFit
        return imageView
    }()
    
    lazy var checkInTimeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 40)
        label.textColor = UIColor.hex("#0F499E")
        label.text = ""
        return label
    }()
    
    lazy var checkSuccessLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.hex("#0F499E")
        label.font = UIFont.systemFont(ofSize: 18)
        label.text = ""
        return label
    }()
    
    lazy var doTaskButton: UIButton = {
        let button = UIButton.createCornerButton("去做任务")
        return button
    }()
    
    lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.hex("#CBD0D9")
        label.text = ""
        return label
    }()
    
    lazy var addressPanel: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.hex("#F3F5F8")
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 8
        return view
    }()
    
    lazy var addressLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.hex("#8D99AC")
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = ""
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    lazy var addressDetailLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = ""
        return label
    }()
}
